package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.Project;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Wechat_user;
import com.wzq.service.IWechat_userService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理-用户Controller控制器
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private IWechat_userService iWechat_userService;

    /**
     * 根据条件分页查询用户信息
     * @param pageBean
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Wechat_user> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Wechat_user> pageResult = iWechat_userService.page(page, new QueryWrapper<Wechat_user>().like(StringUtil.isNotEmpty(query), "nickName", query));
        Map<String,Object> map=new HashMap<>();
        List<Wechat_user> list=new ArrayList<>();
        for (Wechat_user wechat_user : pageResult.getRecords()) {
            if (wechat_user.getSex() == 1)        wechat_user.setSex_c("女");
            else                                  wechat_user.setSex_c("男");
            list.add(wechat_user);
        }
        map.put("userList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iWechat_userService.removeById(id);
        return R.ok();
    }

    @RequestMapping("/listAll")
    public R listAll(){
        Map<String,Object> map=new HashMap<>();
        map.put("userList",iWechat_userService.list());
        return R.ok(map);
    }

}
